﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PedesTrainProject.Properties;
using System.Drawing;
using System.Windows;

namespace PedesTrainProject.Core
{
    public class Visualization
    {
        private Simulation simulation;
        private Graphics e;
        private Graphics anim;
        private System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Winding);
        private Traffic.ITrafficCore coreTraffic;
        private Bitmap temp;
        private GUI.SimulationCreator scGUI;
        

        public Visualization(Simulation _simulation, Graphics _e, GUI.SimulationCreator _scGUI)
        {
            simulation = _simulation;
            e = _e;
            anim = simulation.Pb.CreateGraphics();
            anim.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
            anim.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
            temp = new Bitmap(simulation.DrawArea);
            scGUI = _scGUI;
        }

        public void run()
        {

        }

        public void pause()
        {

        }

        public void stop()
        {

        }

        public void animateCar(Vector position)
        {
           
           
            gp.AddRectangle(new Rectangle((int)position.X, (int)position.Y, 6, 6));
                  
        }

        public void animatePedestrian(Vector position)
        {
            //Brush b = new SolidBrush(Color.BlueViolet);
            gp.AddRectangle(new Rectangle((int)position.X, (int)position.Y, 5, 5));
        }

        public void drawAll()
        {
            lock (this)
            {
                scGUI.updateScreen(gp);
                gp = new System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Winding);
            }
        }

        public void changeLights( int x, int y, Color color, Grid.Directions direction)
        {
            Pen carPen = new Pen(Color.BlueViolet);
            Pen pen1 = new Pen(Color.Black);
            Pen red = new Pen(Color.Red, 3);
            Pen green = new Pen(Color.Green, 3);
            Pen yellow = new Pen(Color.Yellow, 3);
            Brush greenBrush = new SolidBrush(Color.Green);
            Brush redBrush = new SolidBrush(Color.Red);
            Brush yellowBrush = new SolidBrush(Color.Yellow);
            Brush blackBrush = new SolidBrush(Color.Black);

            
            lock (this)
            {
                if (direction == Grid.Directions.North)
                {
                    
                    e.FillRectangle(blackBrush, x + 15, y + 2, 9, 20);
                    if (color == Color.Green)
                        e.DrawEllipse(green, x + 17, y + 4, 3, 3);
                    if (color == Color.Yellow)
                        e.DrawEllipse(yellow, x + 17, y + 10, 3, 3);
                    if (color == Color.Red)
                        e.DrawEllipse(red, x + 17, y + 16, 3, 3);
                }

                int o = 62;
                if (direction == Grid.Directions.South)
                {
                    
                    e.FillRectangle(blackBrush, x + 15 + o, y + 2 + o + 12, 9, 20);
                    if (color == Color.Green)
                        e.DrawEllipse(green, x + 17 + o, y + 4 + o + 12, 3, 3);
                    if (color == Color.Yellow)
                        e.DrawEllipse(yellow, x + 17 + o, y + 10 + o + 12, 3, 3);
                    if (color == Color.Red)
                        e.DrawEllipse(red, x + 17 + o, y + 16 + o + 12, 3, 3);
                }
                if (direction == Grid.Directions.West)
                {
                    
                    e.FillRectangle(blackBrush, x + 3, y + 15 + o, 20, 9);
                    if (color == Color.Green)
                        e.DrawEllipse(green, x + 4, y + 17 + o, 3, 3);
                    if (color == Color.Yellow)
                        e.DrawEllipse(yellow, x + 10, y + 17 + o, 3, 3);
                    if (color == Color.Red)
                        e.DrawEllipse(red, x + 16, y + 17 + o, 3, 3);
                }
                if (direction == Grid.Directions.East)
                {
                    
                    e.FillRectangle(blackBrush, x + 14 + o, y + 15, 20, 9);
                    if (color == Color.Green)
                        e.DrawEllipse(green, x + 4 + o + 12, y + 17, 3, 3);
                    if (color == Color.Yellow)
                        e.DrawEllipse(yellow, x + 10 + o + 12, y + 17, 3, 3);
                    if (color == Color.Red)
                        e.DrawEllipse(red, x + 16 + o + 12, y + 17, 3, 3);
                }

            }
            
        }
    }
}
